﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KAKA.Infrastructure
{
    public class BirthdayModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            return base.BindModel(controllerContext, bindingContext);


        }

        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Name == "FinalBirthday")
            {
                var birthdayFields = new string[] { "BirthYear", "BirthMonth", "BirthDay" };
                var propertyDescriptors = this.GetTypeDescriptor(controllerContext, bindingContext).GetProperties();

                foreach (PropertyDescriptor descriptor in propertyDescriptors)
                {
                    if (birthdayFields.Contains(descriptor.Name))
                    {

                    }
                }

                object value = propertyDescriptor.GetValue(bindingContext.Model);
                var binder = this.Binders.GetBinder(propertyDescriptor.PropertyType);
                var modelMetadata = bindingContext.PropertyMetadata["BirthDay"];

                var birthDay = this.GetPropertyValue(controllerContext, new ModelBindingContext
                {
                    ModelName = "BirthDay",
                    ModelMetadata = bindingContext.ModelMetadata,
                    ValueProvider = bindingContext.ValueProvider,
                    ModelState = bindingContext.ModelState,
                }, propertyDescriptor, binder);
            }


            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}